package com.skyline.wo.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.skyline.base.exception.NoResourceException;
import com.skyline.common.bean.Page;
import com.skyline.common.util.FreeMarkerUtils;
import com.skyline.feed.service.PersonalFeedService;
import com.skyline.feed.type.FeedType;
import com.skyline.wo.dao.VideoDao;
import com.skyline.wo.model.Video;
import com.skyline.wo.service.VideoService;
import com.skyline.wo.service.VideoUtil;

@Service("videoService")
public class VideoServiceImpl implements VideoService {

	@Autowired
	private VideoDao videoDao;

	@Autowired
	private PersonalFeedService personalFeedService;
	
	private int pageSize=2;

	@Override
	public Video queryVideoInfo(String url) throws Exception {
		return VideoUtil.getVideoInfo(url);
	}

	@SuppressWarnings("deprecation")
	@Override
	public long addVideo(Long ownerId, String ownerPortrait, String ownerNickname, String title, String thumbnail, String summary,
			String time, String source, String pageUrl, String flashUrl, String htmlCode) {
		long id = videoDao.insertVideo(ownerId, ownerPortrait, ownerNickname, title, thumbnail, summary, time, source, pageUrl, flashUrl,
				htmlCode);
		String digest = null;
		Map<String, Object> valueMap = new HashMap<String, Object>();
		valueMap.put("ownerId", ownerId);
		valueMap.put("videoId", id);
		valueMap.put("flashUrl", flashUrl);
		valueMap.put("thumbnail", thumbnail);
		valueMap.put("videoSummary", summary);
		digest = FreeMarkerUtils.getVideoDigest(valueMap);
		//videoDao.updateVideoDigest(id, digest);
		 personalFeedService.addFeed(id, title, digest, ownerId, ownerNickname, ownerPortrait, ownerId, FeedType.VIDEO);
		return id;
	}

	@Override
	public Video getVideoById(Long id) throws NoResourceException {
		Video video = videoDao.queryVideoById(id);
		videoDao.updateVideoVisitNum(id);
		return video;
	}

	@Override
	public List<Video> getVideoByOwnerId(Long ownerId,Page page){
		
		page.setSize(pageSize);
		return videoDao.queryVideoByOwnerId(ownerId, page);
	}
}
